package com.altekis.kanbankaze.client;

import com.altekis.kanbankaze.client.ui.EditableLabel;
import com.altekis.kanbankaze.shared.Task;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style;
import com.google.gwt.dom.client.Style.Overflow;
import com.google.gwt.dom.client.Style.Position;
import com.google.gwt.event.dom.client.HasAllMouseHandlers;
import com.google.gwt.event.dom.client.HasTouchEndHandlers;
import com.google.gwt.event.dom.client.HasTouchStartHandlers;
import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.event.dom.client.MouseMoveEvent;
import com.google.gwt.event.dom.client.MouseMoveHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.event.dom.client.MouseUpEvent;
import com.google.gwt.event.dom.client.MouseUpHandler;
import com.google.gwt.event.dom.client.MouseWheelEvent;
import com.google.gwt.event.dom.client.MouseWheelHandler;
import com.google.gwt.event.dom.client.TouchEndEvent;
import com.google.gwt.event.dom.client.TouchEndHandler;
import com.google.gwt.event.dom.client.TouchStartEvent;
import com.google.gwt.event.dom.client.TouchStartHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

/**
 * Card view of a Task
 * 
 */
public class TaskCardView extends Composite implements HasTouchStartHandlers, HasTouchEndHandlers,
		HasAllMouseHandlers, TaskCardPresenter.Display {

	public static final String CLASSNAME = "lifted";
	private boolean expanded = false;

	interface TaskCardViewUiBinder extends UiBinder<Widget, TaskCardView> {
	}

	private static TaskCardViewUiBinder uiBinder = GWT
			.create(TaskCardViewUiBinder.class);

	@UiField
	EditableLabel descriptionField;
	@UiField
	Button editButton;
	@UiField
	EditableLabel nameField;
	protected Task task;
	@UiField
	Label titleField;
	@UiField
	AbsolutePanel avatarPanel;
	
	public TaskCardView(Task task) {
		this.task = task;
		initWidget(uiBinder.createAndBindUi(this));
		getElement().getStyle().setOverflow(Overflow.VISIBLE);
		getElement().getFirstChildElement().getStyle().setPosition(Position.ABSOLUTE);
		getElement().getFirstChildElement().getStyle().setOverflow(Overflow.VISIBLE);
		avatarPanel.getElement().getStyle().setPosition(Position.ABSOLUTE);
		this.getElement().getFirstChildElement().addClassName(this.getTask().getColor());
	}			


	public HandlerRegistration addMouseDownHandler(MouseDownHandler handler) {
		return addDomHandler(handler, MouseDownEvent.getType());
	}

	public HandlerRegistration addMouseMoveHandler(MouseMoveHandler handler) {
		return addDomHandler(handler, MouseMoveEvent.getType());
	}

	public HandlerRegistration addMouseOutHandler(MouseOutHandler handler) {
		return addDomHandler(handler, MouseOutEvent.getType());
	}

	public HandlerRegistration addMouseOverHandler(MouseOverHandler handler) {
		return addDomHandler(handler, MouseOverEvent.getType());
	}

	public HandlerRegistration addMouseUpHandler(MouseUpHandler handler) {
		return addDomHandler(handler, MouseUpEvent.getType());
	}

	public HandlerRegistration addMouseWheelHandler(MouseWheelHandler handler) {
		return addDomHandler(handler, MouseWheelEvent.getType());
	}

	@Override
	public HandlerRegistration addTouchEndHandler(TouchEndHandler handler) {
		return addDomHandler(handler, TouchEndEvent.getType());
	}

	@Override
	public HandlerRegistration addTouchStartHandler(TouchStartHandler handler) {
		return addDomHandler(handler, TouchStartEvent.getType());
	}

	@Override
	public EditableLabel getDescriptionField() {
		return descriptionField;
	}

	@Override
	public Button getEditButton() {
		return editButton;
	}

	@Override
	public EditableLabel getNameField() {
		return nameField;
	}

	public Task getTask() {
		return task;
	}

	@Override
	public HasText getTitleField() {
		return titleField;
	}

	public void setPixelPosition(int x, int y) {
		final Style style = this.getElement().getFirstChildElement().getStyle();
		style.setProperty("left", x / 10 + "em");
		style.setProperty("top", y / 10 + "em");
	}

	public void setTask(Task task) {
		this.task = task;
	}

	@Override
	public void expandCollapse() {
		if (expanded) {
			this.removeStyleName("expanded");
		} else {
			this.addStyleName("expanded");
		}
		expanded = !expanded;
	}


	@Override
	public AbsolutePanel getAvatarPanel() {
		return this.avatarPanel;
	}

}
